package k_singleton.a_design_pattern.b_thread_safe_lazy;

/**
 * Created by wug on 2016/7/15 0015.
 * 懒汉式，线程安全
 */
public class Singleton {
    private static Singleton instance;
    private Singleton (){}

    /**
     * 效率低的原因就在于 在任何时候都要同步 但是其实如果不是null的时候返回就行了
     * @return
     */
    public static synchronized Singleton getInstance() {
        if (instance == null) {
            try {
                Thread.sleep(1200L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            instance = new Singleton();
        }
        return instance;
    }

    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}
